package com.cbarmstrong.Finance;

import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;

public class CrossOverIndicator implements Indicator {

	HashMap<Date, Integer> indicatorValues=new HashMap<Date, Integer>();
	private String representation;
	
	@SuppressWarnings("unchecked")
	public CrossOverIndicator(String i1, String i2, Ticker ticker){
		//Data.getLogger().fine("Creating CrossOverIndicator between:");
		//Data.getLogger().fine(i1);
		//Data.getLogger().fine(i2);
		
		Vector<Double> diff = Data.differenceSet(Indicators.getIndicator(i1, ticker).getValues(), Indicators.getIndicator(i2, ticker).getValues(), new Vector<Double>());
		representation="cross "+i1+" "+i2;
		for(int i=0;i<diff.size();i++){
			try{
				int position=0;
				if(diff.get(i)>0){ position=1; }
				if(diff.get(i)<0){ position=-1; }
				indicatorValues.put(ticker.getDates().get(i), position);
			} catch(NullPointerException e){
				indicatorValues.put(ticker.getDates().get(i), 0);
			}
		}
	}

	public Integer position(Date date) {
		//Data.getLogger().fine("Value at "+date+": "+indicatorValues.get(date));
		//Data.getLogger().fine("For "+representation);
		return indicatorValues.get(date);
	}

	@SuppressWarnings("rawtypes")
	public Vector getValues() {
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public void toGraph(Ticker ticker, OutputStream out, int x, int y){
		Vector<Double> i1=Indicators.getIndicator(representation.split(" ")[1], ticker).getValues();
		Vector<Double> i2=Indicators.getIndicator(representation.split(" ")[2], ticker).getValues();
		TimeSeriesCollection dataset=new TimeSeriesCollection();
		TimeSeries t1=new TimeSeries("1");
		TimeSeries t2=new TimeSeries("2");
		ArrayList<Color> colours = new ArrayList<Color>();
		int seriesCount=3;
		int state=position(ticker.getDates().get(0));
		if(state==1){ colours.add(Color.green); }
		if(state==0){ colours.add(Color.black); }
		if(state==-1){ colours.add(Color.red); }
		JFreeChart chart = ChartFactory.createTimeSeriesChart(representation, "Time", "Values", dataset, false, false, false);
		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setRenderer(new XYSplineRenderer(50));
		for(int i=0;i<ticker.getDates().size();i++){
			if(state!=position(ticker.getDates().get(i))){
				t1.add(new TimeSeriesDataItem(new Day(ticker.getDates().get(i)), i1.get(i)));
				state=position(ticker.getDates().get(i));
				if(state==1){ colours.add(Color.green); }
				if(state==0){ colours.add(Color.black); }
				if(state==-1){ colours.add(Color.red); }
				try {
					dataset.addSeries((TimeSeries) t1.clone());
				} catch (CloneNotSupportedException e) {
					e.printStackTrace();
				}
				t1=new TimeSeries(seriesCount++);
			}
			t1.add(new TimeSeriesDataItem(new Day(ticker.getDates().get(i)), i1.get(i)));
			t2.add(new TimeSeriesDataItem(new Day(ticker.getDates().get(i)), i2.get(i)));
		}
		dataset.addSeries(t1);
		dataset.addSeries(t2);
		int j=0;
		((XYLineAndShapeRenderer) plot.getRenderer()).setSeriesShapesVisible(plot.getSeriesCount()-1, false);
		plot.getRenderer().setSeriesPaint(plot.getSeriesCount()-1, Color.black);
		for(int i=0;i<plot.getSeriesCount()-1;i++){
			((XYLineAndShapeRenderer) plot.getRenderer()).setSeriesShapesVisible(i, false);
			plot.getRenderer().setSeriesPaint(i, colours.get(j++));
		}
		try {
			ChartUtilities.writeChartAsJPEG(out, chart, x, y);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public int days() {
		return indicatorValues.size();
	}
	
	public String save(){
		return representation;
	}
	
}
